Get Started with OCR in C# and VB.NET
IronOCR 是一個 C# 軟體庫,允許 .NET 平台的軟體開發人員從圖像和 PDF 文件中識別和讀取文本。 它是一個純 .NET 的 OCR 庫,使用了已知最先進的 Tesseract 引擎。
安裝
使用 NuGet 套件管理器安裝
在 Visual Studio 或命令行中使用 NuGet Package Manager 安裝 IronOcr。 在 Visual Studio 中,導航至控制台:
- 工具 ->
- NuGet 包管理器 ->
- 包管理控制台
Install-Package IronOcr
查看 IronOcr 在 NuGet 上 以獲取更多版本更新和安裝信息。
還有其他適用於不同平台的 IronOCR NuGet Packages:
IronOcr.Extensions.AdvancedScan 適用於 Linux 和 macOS
此套件適用於使用 Linux 和 Mac 並希望獲得更多 IronOcr 高級功能的用戶。
- Linux: https://www.nuget.org/packages/IronOcr.Extensions.AdvancedScan.Linux
- MacOS: https://www.nuget.org/packages/IronOcr.Extensions.AdvancedScan.MacOs
疑難排解
在此套件的最新更新中,IronOCR 將 OpenCV 的依賴項合併到套件中以簡化安裝,因此,如果開發者目前正在導入 OpenCV 依賴項則會出現以下錯誤。
The type of namespace name `OpenCvSharp` could not be found(are you missing a using directive or an assembly reference)
您可以安全地移除 OpenCV 命名空間問題即可解決。
下載 IronOCR .ZIP
您也可以選擇通過 .ZIP 文件下載 IronOCR。 點擊此處直接下載 DLL。 一旦下載了 .zip 文件:
.NET Framework 4.0+ 安裝說明:
- 將 net40 文件夾中的 IronOcr.dll 包含到您的項目中
-
然後添加程序集引用:
- System.Configuration
- System.Drawing
- System.Web
.NET Standard & .NET Core 2.0+, & .NET 5 說明
- 在項目中包含 netstandard2.0 文件夾中的 IronOcr.dll
-
然後添加 NuGet Package 引用:
- System.Drawing.Common 4.7 或更高版本
下載 IronOCR 安裝程式(僅限 Windows)
另一個選擇是下載我們的 IronOCR 安裝程式,將安裝 IronOCR 要求的所有資源,以便開箱即用。 請注意,此選項僅適用於 Windows 系統。 要下載安裝程式,請點擊此處。 一旦下載了 .zip 文件:
.NET Framework 4.0+ 安裝說明:
- 將 net40 文件夾中的 IronOcr.dll 包含到您的項目中
-
然後添加程序集引用:
- System.Configuration
- System.Drawing
- System.Web
.NET Standard & .NET Core 2.0+, & .NET 5 說明
- 在項目中包含 netstandard2.0 文件夾中的 IronOcr.dll
-
然後添加 NuGet Package 引用:
- System.Drawing.Common 4.7 或更高版本
為什麼選擇 IronOCR?
IronOCR 是一個易於安裝、完整並有良好文檔記錄的 .NET 軟體庫。
選擇 IronOCR 可實現99.8%+ 的 OCR 準確度,而無需使用任何外部網路服務、持續費用或通過互聯網發送機密文件。
為何 C# 開發者選擇 IronOCR 而不是 Vanilla Tesseract:
- 安裝為單個 DLL 或 NuGet
- 開箱即用:包括 Tesseract 5, 4 和 3 發動機。
- 準確度達到 99.8%,明顯超過普通 Tesseract。
- 速度快且支持多線程
- MVC、WebApp、桌面、控制台及服務器應用程序兼容
- 無需處理 Exe 或 C++ 代碼
- 完整的 PDF OCR 支持
- 幾乎對所有圖像文件或 PDF 執行 OCR
- 完整的 .NET Core、Standard 和 Framework 支持
- 可部署在 Windows、Mac、Linux、Azure、Docker、Lambda、AWS
- 讀取條碼和 QR 碼
- 將 OCR 結果導出為 XHTML
- 將 OCR 導出為可搜索的 PDF 文檔
- 支持多線程
- 125 種國際語言,所有語言包皆可通過 NuGet 或 OcrData 文件管理
- 提取圖像、坐標、統計和字體。 不僅是文本。
- 可用於在商用和專有應用中重新分發 Tesseract OCR。
IronOCR 在處理真實世界圖像和不完美文件時表現突出,例如含有數字噪點或不完整的低分辨率照片或掃描件。
其他適用於 .NET 平台的免費 OCR庫如 .NET Tesseract API 和網絡服務在這些真實用例中表現不佳。
使用 Tesseract 5 的 OCR - 用 C# 開始編碼
下面的代碼示例展示了使用 C# 或 VB.NET 從圖像中讀取文本的簡單性。
簡易示例
:path=/static-assets/ocr/content-code-examples/get-started/get-started-1.cs
string Text = new IronTesseract().Read(@"img\Screenshot.png").Text;
Dim Text As String = (New IronTesseract()).Read("img\Screenshot.png").Text
可配置的 Hello World
:path=/static-assets/ocr/content-code-examples/get-started/get-started-2.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
// Add multiple images
input.LoadImage("images/sample.jpeg");
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
' Add multiple images
input.LoadImage("images/sample.jpeg")
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
C# PDF OCR
同樣的方法也可以用來從任何 PDF 文檔中提取文本。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-3.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
// We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password: "password");
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
// 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages");
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
' We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password:= "password")
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
' 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages")
多頁 TIFF 的 OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-4.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("multi-frame.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("multi-frame.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
條碼和 QR 碼
〈OcrResult.OcrBarcode〉類的實例為開發者提供了有關每個掃描條形碼的詳細信息。 AWS Textract 不提供此功能。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-5.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
input.LoadImage("img/Barcode.png");
OcrResult Result = ocr.Read(input);
foreach (var Barcode in Result.Barcodes)
{
// type and location properties also exposed
Console.WriteLine(Barcode.Value);
}
Imports IronOcr
Private ocr As New IronTesseract()
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
input.LoadImage("img/Barcode.png")
Dim Result As OcrResult = ocr.Read(input)
For Each Barcode In Result.Barcodes
' type and location properties also exposed
Console.WriteLine(Barcode.Value)
Next Barcode
End Using
圖像特定區域的 OCR
所有 IronOCR 的掃描和閱讀方法都提供指明我們希望閱讀哪一頁或哪些頁面特定部位的能力。 這在查看標準化表單時非常有用,可以節省大量時間並提高效率。
要使用裁剪區域,我們需要添加一個系統引用到 System.Drawing,以便可以使用 System.Drawing.Rectangle 對象。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-6.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
// Dimensions are in pixel
var contentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };
input.LoadImage("document.png", contentArea);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
' Dimensions are in pixel
Private contentArea = New System.Drawing.Rectangle() With {
.X = 215,
.Y = 1250,
.Height = 280,
.Width = 1335
}
input.LoadImage("document.png", contentArea)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
低質量掃描的 OCR
IronOCR OcrInput 類可以修正普通 Tesseract 無法讀取的掃描件。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-7.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);
// fixes digital noise and poor scanning
input.DeNoise();
// fixes rotation and perspective
input.Deskew();
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)
' fixes digital noise and poor scanning
input.DeNoise()
' fixes rotation and perspective
input.Deskew()
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
將 OCR 結果導出為可搜索的 PDF
:path=/static-assets/ocr/content-code-examples/get-started/get-started-8.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Quarterly Report";
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Quarterly Report"
input.LoadImage("image1.jpeg")
input.LoadImage("image2.png")
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("image3.gif", pageindices)
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
從 TIFF 到可搜索 PDF 的轉換
:path=/static-assets/ocr/content-code-examples/get-started/get-started-9.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("example.tiff", pageindices)
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf")
將 OCR 結果導出為 HTML
:path=/static-assets/ocr/content-code-examples/get-started/get-started-10.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Html Title";
input.LoadImage("image1.jpeg");
OcrResult Result = ocr.Read(input);
Result.SaveAsHocrFile("results.html");
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Html Title"
input.LoadImage("image1.jpeg")
Dim Result As OcrResult = ocr.Read(input)
Result.SaveAsHocrFile("results.html")
OCR 圖像增強濾鏡
IronOCR 提供獨特的濾鏡給 OcrInput 對象以改善 OCR 性能。
圖像增強代碼示例
:path=/static-assets/ocr/content-code-examples/get-started/get-started-11.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("LowQuality.jpeg");
// fixes digital noise and poor scanning
input.DeNoise();
// fixes rotation and perspective
input.Deskew();
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.LoadImage("LowQuality.jpeg")
' fixes digital noise and poor scanning
input.DeNoise()
' fixes rotation and perspective
input.Deskew()
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
OCR 圖像濾鏡列表
內建在 IronOCR 中的增強 OCR 性能的輸入濾鏡包括:
OcrInput.Rotate(double degrees)- 將圖像按照順時針的角度數旋轉。逆時針旋轉使用負數。OcrInput.Binarize()- 這種濾鏡將每個像素轉換為黑色或白色而沒有中間地帶,可能會在極低對比度圖像中提高 OCR 性能。OcrInput.ToGrayScale()- 將每個像素轉換為灰度色調。 它可能不會提高準確性,但可以提高速度。OcrInput.Contrast()- 自動增強對比度,通常在低對比度掃描中提高速度和準確性。OcrInput.DeNoise()- 移除數字噪點,僅在噪點預料時推薦使用。OcrInput.Invert()- 反轉每個顏色(白變黑,反之亦然)。OcrInput.Dilate()- 提升形態學,增加像素到對象邊界,與侵蝕相反。OcrInput.Erode()- 提升形態學,從對象邊界移除像素,與擴展相反。OcrInput.Deskew()- 旋轉圖像以正確顯示。 有用,因為 Tesseract 的傾斜容忍度有限。OcrInput.EnhanceResolution- 增強低質量圖像的分辨率。 此設置通常用於自動管理低 DPI 輸入。EnhanceResolution檢測低分辨率圖像(低於 275 dpi),對其進行放大,並銳化文本以獲得更好的 OCR 效果。 雖然耗時,但它通常會減少整體 OCR 操作時間。Language- 支援從 22 個國際語言包中選擇。Strategy- 允許選擇快速且不那麼準確或高級(使用 AI 確保準確)策略,基於詞語的統計關係。ColorSpace- 選擇在灰度或彩色下進行 OCR; 灰度通常最佳,雖然彩色在特定對比情況下可能會更好。DetectWhiteTextOnDarkBackgrounds- 自動調整負向圖像,檢測和讀取在深色背景上的白色文本。InputImageType- 指導OCR庫,指定它是處理完整文檔還是片段。RotateAndStraighten- 允許 IronOCR 正確處理旋轉或受透視偏差影響的文檔。ReadBarcodes- 在文本掃描時自動同時掃描條碼和 QR 碼,沒有顯著的增加時間。ColorDepth- 決定 OCR 過程中色彩深度的每像素位數。 較高的深度可能會增加質量,但也會延長處理時間。
125 語言包
IronOCR supports 125 international languages via language packs which are distributed as DLLs, available for download from this website, or from the NuGet Package Manager.
語言包括德語、法語、英文、中文、日語等。 還有專業語言包適用於 MRZ、MICR 支票、財務數據、車牌等。此外,還可以使用自訂的 tesseract “.traineddata” 文件。
語言範例
// Reference to the path of the source file that demonstrates setting language packs for OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
// Reference to the path of the source file that demonstrates setting language packs for OCR
using IronOcr;
// PM> Install IronOcr.Languages.Arabic
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("img/arabic.gif", pageindices);
// Add image filters if needed
// In this case, even thought input is very low quality
// IronTesseract can read what conventional Tesseract cannot.
OcrResult result = ocr.Read(input);
// Console can't print Arabic on Windows easily.
// Let's save to disk instead.
result.SaveAsTextFile("arabic.txt");
' Reference to the path of the source file that demonstrates setting language packs for OCR
Imports IronOcr
' PM> Install IronOcr.Languages.Arabic
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic
Using input As New OcrInput()
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img/arabic.gif", pageindices)
' Add image filters if needed
' In this case, even thought input is very low quality
' IronTesseract can read what conventional Tesseract cannot.
Dim result As OcrResult = ocr.Read(input)
' Console can't print Arabic on Windows easily.
' Let's save to disk instead.
result.SaveAsTextFile("arabic.txt")
End Using
多重語言範例
也可以同時使用多種語言進行OCR。這可以增強英語元數據和 URL 字符在 Unicode 文檔中的 OCR 性能。
// Reference to the path of the source file that demonstrates multi-language OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
// Reference to the path of the source file that demonstrates multi-language OCR
using IronOcr;
// PM> Install IronOcr.Languages.ChineseSimplified
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;
// We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English);
using OcrInput input = new OcrInput();
input.LoadPdf("multi-language.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("results.txt");
' Reference to the path of the source file that demonstrates multi-language OCR
Imports IronOcr
' PM> Install IronOcr.Languages.ChineseSimplified
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified
' We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English)
Using input As New OcrInput()
input.LoadPdf("multi-language.pdf")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsTextFile("results.txt")
End Using
詳細的 OCR 結果對象
IronOCR 為每次操作返回一個 OCR 結果對象。 通常,開發者訪問 Text 屬性以獲取掃描文本。 然而,結果對象包含了更加詳細的信息。
// Reference to the path of the source file demonstrating detailed OCR result object usage
:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
// Reference to the path of the source file demonstrating detailed OCR result object usage
using IronOcr;
IronTesseract ocr = new IronTesseract();
// Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\sample.tiff", pageindices);
OcrResult result = ocr.Read(input);
var pages = result.Pages;
var words = pages[0].Words;
var barcodes = result.Barcodes;
// Explore here to find a massive, detailed API:
// - Pages, Blocks, Paraphaphs, Lines, Words, Chars
// - Image Export, Fonts Coordinates, Statistical Data, Tables
' Reference to the path of the source file demonstrating detailed OCR result object usage
Imports IronOcr
Private ocr As New IronTesseract()
' Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\sample.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Dim pages = result.Pages
Dim words = pages(0).Words
Dim barcodes = result.Barcodes
' Explore here to find a massive, detailed API:
' - Pages, Blocks, Paraphaphs, Lines, Words, Chars
' - Image Export, Fonts Coordinates, Statistical Data, Tables
End Using
性能
IronOCR 開箱即用,無需性能調校或圖像修改。
速度極快:IronOcr.2020+ 比以前的版本快多達 10 倍且錯誤率減少超過 250%。
進一步了解
要瞭解更多關於 C#、VB、F# 或任何其他 .NET 語言中的 OCR,請閱讀我們的社區教程,提供使用 IronOCR 的真實範例並展示優化該庫的細微差別。
還提供完整的 供 .NET 開發者參考的 API。

